package com.glsc.ngateway.common.base.repo.mysql.gateway.platform;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Url;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Repository
public interface UrlRepo extends JpaRepository<Url, Long>, JpaSpecificationExecutor<Url> {
    //@Lock(value = LockModeType.PESSIMISTIC_WRITE)
    @Query(value = "select u from Url u where u.id = :id and u.deleted = 0")
    Url queryByIdLock(@Param("id") long id);

    @Transactional
    @Modifying
    @Query(value = "update Url u set u.deleted = 1 where u.id = :id")
    int deleteById(@Param("id") long id);

    @Query(value = "select u from Url u where u.deleted = 0 and u.requireAuth = :requireAuth")
    Set<Url> queryAllByRequireAuth(@Param("requireAuth") int requireAuth);

    @Query(value = "select u from Url u where u.name like :name")
    List<Url> findAllByName(@Param("name") String name);

    @Transactional(readOnly = true)
    @Query(value = "select u from Url u where u.deleted = 0")
    List<Url> findAll();


    List<Url> findByName(String name);

    List<Url> findByNameAndDeleted(String name, int deleted);


}
